package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 户口信息功能实现类
 */
@Service
public class HouseholdServiceImpl extends ServiceImpl<HouseholdMapper, Household> implements HouseholdService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的Household表mapper对象
     */
    @Autowired
    private HouseholdMapper _HouseholdMpper;

 			
  	 @Autowired
    private CensusInfoPersonMapper _CensusInfoPersonMapper;   
			
 			


    /**
     * 户口信息分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<HouseholdDto> List(HouseholdPagedInput input) {

      
        //声明一个支持户口信息查询的(拉姆达)表达式
        LambdaQueryWrapper<Household> queryWrapper = Wrappers.<Household>lambdaQuery()
                .eq(input.getId()!=null,Household::getId,input.getId())
                .eq(input.getCreatorId()!=null,Household::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入FamilyNuberStartRange,则要大于FamilyNuberStartRange输入的值
         if(input.getFamilyNuberStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(Household::getFamilyNuber,input.getFamilyNuberStartRange());
          }
         //如果前端搜索传入FamilyNuberEndRange,则要小于FamilyNuberEndRange输入的值
      		 if(input.getFamilyNuberEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(Household::getFamilyNuber,input.getFamilyNuberEndRange());
          }
     	 //如果前端搜索传入Address不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getAddress())) {
            queryWrapper=queryWrapper.eq(Household::getAddress,input.getAddress());
       	 }
     	 //如果前端搜索传入Code不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getCode())) {
            queryWrapper=queryWrapper.eq(Household::getCode,input.getCode());
       	 }
     	 //如果前端搜索传入Phone不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getPhone())) {
            queryWrapper=queryWrapper.eq(Household::getPhone,input.getPhone());
       	 }
     	 //如果前端搜索传入AuditStatus不为空,则进行精确查询
        if(input.getAuditStatus()!=null) {
            queryWrapper=queryWrapper.eq(Household::getAuditStatus,input.getAuditStatus());
       	 }
     	 //如果前端搜索传入CensusinfoHeadId不为空,则进行精确查询
        if(input.getCensusinfoHeadId()!=null) {
            queryWrapper=queryWrapper.eq(Household::getCensusinfoHeadId,input.getCensusinfoHeadId());
       	 }
     	 //如果前端搜索传入PoliceAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getPoliceAddress())) {
            queryWrapper=queryWrapper.eq(Household::getPoliceAddress,input.getPoliceAddress());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(Household::getCreationTime);
        //构建一个分页查询的model
        Page<Household> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取户口信息数据
        IPage<Household> pageRecords= _HouseholdMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _HouseholdMpper.selectCount(queryWrapper);

        //把Household实体转换成Household传输模型
        List<HouseholdDto> items= Extension.copyBeanList(pageRecords.getRecords(),HouseholdDto.class);
        for (HouseholdDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         

            //查询关联的CensusInfoPerson表信息
            List<CensusInfoPerson>  CensusInfoPersonEntitys= _CensusInfoPersonMapper.selectList(Wrappers.<CensusInfoPerson>lambdaQuery().eq(CensusInfoPerson::getHouseholdId,item.getId()));
            List<CensusInfoPersonDto> CensusInfoPersonDTOS= Extension.copyBeanList(CensusInfoPersonEntitys,CensusInfoPersonDto.class);
            item.setCensusInfoPersonDtos(CensusInfoPersonDTOS);
         
           
          	            
           //查询出关联的CensusInfoPerson表信息
           CensusInfoPersonDto CensusinfoHeadDTO = new CensusInfoPersonDto();        
            CensusInfoPerson  CensusinfoHeadEntity= _CensusInfoPersonMapper.selectOne(Wrappers.<CensusInfoPerson>lambdaQuery().eq(CensusInfoPerson::getId,item.getCensusinfoHeadId()));
           if(CensusinfoHeadEntity!=null) {  
          BeanUtils.copyProperties(CensusinfoHeadDTO,CensusinfoHeadEntity);
            item.setCensusinfoHeadDto(CensusinfoHeadDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个户口信息查询
     */
    @SneakyThrows
    @Override
    public HouseholdDto Get(HouseholdPagedInput input) {
       if(input.getId()==null)
        {
            return new HouseholdDto();
        }
        PagedResult<HouseholdDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new HouseholdDto();
    }

    /**
     *户口信息创建或者修改
     */
    @SneakyThrows
    @Override
    public HouseholdDto CreateOrEdit(HouseholdDto input) {

        //声明一个户口信息实体
        Household Household = new Household();

        //把前端传入的input参数拷贝到户口信息实体
        BeanUtils.copyProperties(Household,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(Household);

        //定义一个返回给前端的户口信息传输模型
        HouseholdDto HouseholdDto=new HouseholdDto();

        //同理把操作的户口信息实体拷贝给户口信息传输模型
        BeanUtils.copyProperties(HouseholdDto,Household);

        //把传输模型返回给前端
        return HouseholdDto;
    }



}
